<template>
	<div id="teacher-list">
		<el-form :inline="true" class="demo-form-inline">
			<el-form-item label="联系信息">
				<el-input v-model="form.name" placeholder="请输入"></el-input>
			</el-form-item>
			<el-form-item label="机构名称">
				<casader :pid="form.pid" ref="child"></casader>
			</el-form-item>
			<el-form-item>
				<el-button type="blue" @click="onSubmit">查询</el-button>
				<el-button type="gray" @click="downLoads">清空</el-button>
			</el-form-item>
			<el-form-item class="fr">
				<el-button type="yellow">添加讲师</el-button>
				<el-button type="blue">数据导出</el-button>
				<el-button type="cyan">批量删除</el-button>
			</el-form-item>
		</el-form>
		<div class="table">
			 <el-table :data="tableData" style="width: 100%"   @selection-change="handleSelectionChange">
				<el-table-column type="index" label="序号" width="70"></el-table-column>
				<el-table-column prop="name"  label="姓名"></el-table-column>
				<el-table-column prop="name"  label="省份"></el-table-column>
				<el-table-column prop="name"  label="账号"></el-table-column>
				<el-table-column prop="name"  label="联系电话"></el-table-column>
				<el-table-column prop="name"  label="机关单位"></el-table-column>
				<el-table-column prop="name"  label="课程次数"></el-table-column>
				<el-table-column label="操作" align="center" width="200">
					<template slot-scope="scope">
						<el-button
						    size="mini"
						    type="blue"
						    @click="goInfo(scope.row.id)"
                        >
                            详情
                        </el-button>
						<el-button
						    size="mini"
						    type="orange"
						    @click="goInfo(scope.row.id)"
                        >
                            初始化密码
                        </el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
			    v-if="tableData.length>0"
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="currentPage"
				:page-size="pageSize"
				layout="total, prev, pager, next, jumper"
				:total="totalCount"
			></el-pagination>
		</div>
	</div>
</template>

<script>
import Casader from '../components/cascaderD';
	export default {
  	components: { Casader },
		 data() {
            return {
				//是否读取完pid
				isPid:false,
				//删除数组
				ids:[],
				//添加
				dialogAdd:false,
				form:{
					name:'',
					nickname:'',
					pid:0
				},
				//添加列表
				formAdd:{
					//单位名
					name:'',
					//单位id
					pid:'',
					phone:'',
					password:'',
					//角色id
					roleId:'',
					//管理人
					pname:'',
				},
				//添加列表
				formEdit:{
					id:'',
					//单位名
					name:'',
					//单位id
					pid:'',
					phone:'',
					password:'',
					//角色id
					roleId:'',
					//管理人
					pname:'',
				},
				//权限列表
				roleOption:[],
				//修改
				dialogEdit:false,
				currentPage: 1,
				pageSize: 10,
				totalCount: 1000,
				tableData:[
                    {
                        name:'张三'
                    }
                ],
				aid:0,
            }
		},
		created() {
			this.init();
		},
		methods:{
			init(){
				let self =this;
				this.getPid();

			},
			rowpassword(id){
				let self = this;
				self.$http.get(`/admin/reset?id=${id}`).then((response) => {
                    let res = response.data
                    if(res.code == '200') {
						self.$message.success(res.message)

                    }else {
						self.$message.error(res.message)
					}


                })
			},
			//获取pid
			getPid(){
				let self = this;
				self.$http.get('/admin/pid').then((response) => {
                    let res = response.data
                    if(res.code == '200') {
						console.log(res,111222)
						self.isPid = true;
						self.form.pid = res.data;
						console.log(self.form.pid,2222222);
						self.$nextTick(function(){
							self.getList();
						});

                    }else {
						self.$message.error(res.message)
					}


                })
			},
			//获取权限列表
			getRole(){
				let self = this;
				self.$http.get('/admin/role').then((response) => {
                    let res = response.data
                    if(res.code == '200') {
						console.log(res,111222)
						self.roleOption = res.data;

                    }else {
						self.$message.error(res.message)
					}


                })
			},
			changeId(val,b){
				console.log(val,22222);
				console.log(b,22222);
			},
			handleItemChange(val) {
				console.log(val,111);
			},
			//查询下级单位
			getSelect(id){
				let self = this;
				if(!id){
					id = '';
				}
				self.$http.get('/learn/class/sub?id='+id).then((response) => {
                    let res = response.data
                    if(res.code == '200') {
						// data:[
						// 	{
						// 		name:'',
						// 		id:1
						// 	}
						// ]
						self.tableData = res.data.content
						self.totalCount = res.data.total
						console.log(self.tableData,111)

                    }else {
						self.$message.error(res.message)
					}


                })
			},
			//获取列表
			getList(){
				let self = this;
				let name = self.form.name;
				let nickname = self.form.nickname;
				let pid = self.$refs.child.cx();
				console.log(pid,55665);
				self.$http.post('/user/list',{
					//?name='+name+ '&nickname='+nickname+'&pid='+pid+'&pageNum='+self.currentPage+'&pageSize='+self.pageSize
					name:name,
					nickname:nickname,
					pid:pid,
					pageNum:self.currentPage,
					pageSize:self.pageSize
				}).then((response) => {
                    let res = response.data
                    if(res.code == '200') {
						console.log(res,1);
						self.tableData = res.data.content
						self.totalCount = res.data.total
						console.log(self.tableData,111)

                    }else {
						self.$message.error(res.message)
					}


                })
			},
			goInfo(id){
				this.$router.push({
					path:'UserInfo',
					query:{id:id}
				})
			},
			handleSelectionChange(rows){
				console.log(rows,1111);
				this.ids = rows.map(item => item.id);
			},
			//查询
			onSubmit(){
				this.currentPage = 1;
				this.getList();
			},
			//下载
            downLoads(){
                let self = this
                // if(!self.tableData.length){
                //     self.$message.error('暂无下载数据');
				// }
				let name = self.form.name;
				let nickname = self.form.nickname;
				let pid = self.$refs.child.cx();
				const loading = self.$loading({
					lock: true,
					text: '加载中...',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				});
                self.$http.post('/user/download',{
					name:name,
					nickname:nickname,
					pid:pid
                },{responseType: 'blob'}) .then((response) => {
                    let blob = new Blob([response.data], {type: response.headers['content-disposition']});
                    //console.log(response.headers['content-disposition'],55555);
                    let fileName = '用户列表.xlsx'
                    let url = window.URL.createObjectURL(blob);
                    let downloadElement = document.createElement('a');
                    downloadElement.style.display = 'none';
                    downloadElement.href = url;
                    downloadElement.download = fileName; //下载后文件名
                    document.body.appendChild(downloadElement);
                    downloadElement.click(); //点击下载
                    document.body.removeChild(downloadElement); //下载完成移除元素
					window.URL.revokeObjectURL(url);
					loading.close();
					console.log(1234);
                }).catch(err => {
                    console.log(err)
					loading.close();
                })
            },
			//分页
			handleSizeChange: function (size) {
				this.pageSize = size;
			},
			handleCurrentChange: function(currentPage) {
				this.currentPage = currentPage
				this.getList();
			},
		},
	}
</script>

<style lang="scss">
#teacher-list{
}
</style>
